package cn.ffcs.uec.view.util;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class SimpleDESCry
{
  public static final String DES_ALGORITHM = "DES";
  public static final String PRIVATE_KEY = "FFCS_EAM2014";
  private static final int SOURCE_STRING_MAX_LEN = 2048;
  private static final int ENCRYPT_STRING_MAX_LEN = 2048;
  private static final Charset default_charset = Charset.forName("UTF-8");
  private static final StringBuffer str_set = new StringBuffer("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_=+{}[]|':;.>,<?/`~!@#$%^&*()");
  private static final StringBuffer str_pw_set = new StringBuffer("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
  private static final char ch_double_quotes = '"';
  StringBuffer des = new StringBuffer();
  StringBuffer org = new StringBuffer();
  StringBuffer errormsg = new StringBuffer();
  
  public StringBuffer cry(StringBuffer src2, String privateKey)
  {
    byte[] src = src2.toString().getBytes(default_charset);
    StringBuffer tmpch = new StringBuffer();
    
    int n_invalid = 1;
    int i = src.length;
    int l_set = str_set.length();
    


    this.des.setLength(0);
    for (int k = 0; k < i; k++)
    {
      String s = String.format("%1$02x", new Object[] { Integer.valueOf(src[k] ^ privateKey.getBytes(default_charset)[(k % privateKey.length())]) });
      this.des.append(s);
    }
    int k=i;
    while (k < 16)
    {
      byte[] des2 = this.des.toString().getBytes(default_charset);
      byte[] key2 = privateKey.getBytes(default_charset);
      
      String s = String.format("%1$02x", new Object[] { Integer.valueOf(key2[(k % privateKey.length())]) });
      this.des.append(s);
      k++;
    }
    return this.des;
  }
  
  public StringBuffer decry(StringBuffer src, String privateKey)
  {
    int k = 0;
    

    List<Byte> list = new ArrayList();
    for (int i = 0; i < src.length(); i += 2)
    {
      String s = src.substring(i, i + 2);
      int hex = hexstrtodec(new StringBuffer(s));
      byte b = (byte)hex;
      b = (byte)(b ^ privateKey.getBytes(default_charset)[(k % privateKey.length())]);
      if (b == 0) {
        break;
      }
      list.add(Byte.valueOf(b));
      
      k++;
    }
    Byte[] bytes = (Byte[])list.toArray(new Byte[list.size()]);
    byte[] bytes2 = new byte[bytes.length];
    for (int i = 0; i < bytes.length; i++) {
      bytes2[i] = bytes[i].byteValue();
    }
    String ss = new String(bytes2, default_charset);
    return new StringBuffer(ss);
  }
  
  int hexstrtodec(StringBuffer strhex)
  {
    int v = 0;
    int total = 0;
    int l = strhex.length();
    for (int i = 0; i < l; i++)
    {
      switch (strhex.charAt(i))
      {
      case '0': 
        v = 0; break;
      case '1': 
        v = 1; break;
      case '2': 
        v = 2; break;
      case '3': 
        v = 3; break;
      case '4': 
        v = 4; break;
      case '5': 
        v = 5; break;
      case '6': 
        v = 6; break;
      case '7': 
        v = 7; break;
      case '8': 
        v = 8; break;
      case '9': 
        v = 9; break;
      case 'a': 
        v = 10; break;
      case 'b': 
        v = 11; break;
      case 'c': 
        v = 12; break;
      case 'd': 
        v = 13; break;
      case 'e': 
        v = 14; break;
      case 'f': 
        v = 15;
      }
      total = total * 16 + v;
    }
    return total;
  }
  
  public static String encry(String data)
  {
    SimpleDESCry sdc = new SimpleDESCry();
    String enData = sdc.cry(new StringBuffer(data), "FFCS_EAM2014").toString();
    return enData;
  }
  
  public static String encry(String data, String privateKey)
  {
    SimpleDESCry sdc = new SimpleDESCry();
    String enData = sdc.cry(new StringBuffer(data), privateKey).toString();
    return enData;
  }
  
  public static String decry(String enData)
  {
    SimpleDESCry sdc = new SimpleDESCry();
    StringBuffer data = sdc.decry(new StringBuffer(enData), "FFCS_EAM2014");
    return data.toString();
  }
  
  public static String decry(String enData, String privateKey)
  {
    SimpleDESCry sdc = new SimpleDESCry();
    StringBuffer data = sdc.decry(new StringBuffer(enData), privateKey);
    return data.toString();
  }
  public static final void main(String[] args)
  {
    String str = "123456";
   /* for (int i = 0; i < str.length(); i++) {
      System.out.println(str);
    }*/
    SimpleDESCry simpleDESCry =new SimpleDESCry();
    String aa=simpleDESCry.encry(str);
    System.out.println("aa----------"+aa);//777470676a73414d3230313446464353
  }
}
